//
//  HomeGridCell.swift
//  d11
//
//  Created by peale on 2018/8/7.
//  Copyright © 2018年 Tech_001. All rights reserved.
//

import UIKit

class HomeGridCell: UITableViewCell{
    var gridBtn1 : UIButton?
    var gridImgView1: UIImageView?
    var gridLabel1: UILabel?
    var girdLineView1 : UIView?
    var gridStatusImgView1: UIImageView?
    
    var gridBtn2 : UIButton?
    var gridImgView2: UIImageView?
    var gridLabel2: UILabel?
    var girdLineView2 : UIView?
    var gridStatusImgView2: UIImageView?
    
    var gridBtn3 : UIButton?
    var gridImgView3: UIImageView?
    var gridLabel3: UILabel?
      var girdLineView3 : UIView?
    var gridStatusImgView3: UIImageView?
    
    var gridBtn4 : UIButton?
    var gridImgView4: UIImageView?
    var gridLabel4: UILabel?
    var gridStatusImgView4: UIImageView?
    var girdLineViewX :UIView?
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    func setUpUI() -> Void {
        self.backgroundColor  = UIColor.white
        self.selectionStyle = UITableViewCell.SelectionStyle.none
        
        let gridBtn1 = UIButton.init(type: .custom)
        self.contentView.addSubview(gridBtn1)
        gridBtn1.snp.makeConstraints{(make) in
            make.width.equalTo(kScreenWidth/4)
            make.height.equalToSuperview()
            make.top.equalToSuperview()
        }
        self.gridBtn1 = gridBtn1
        
        
        self.gridImgView1 = UIImageView.init(image: imageNamed("img_default_60"))
        self.gridBtn1?.addSubview(self.gridImgView1!)
        self.gridImgView1?.snp.makeConstraints{(make) in
            make.width.equalTo(33)
            make.height.equalTo(33)
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(15)
        }
        self.gridLabel1 = UILabel.init()
        self.gridLabel1?.font = UIFont.systemFont(ofSize: 14)
        gridLabel1?.textColor = UIColor.black
        gridLabel1?.textAlignment = .center
        self.gridBtn1?.addSubview(self.gridLabel1!)
        self.gridLabel1?.snp.makeConstraints{(make) in
            make.width.equalToSuperview()
            make.height.equalTo(20)
            make.centerX.equalToSuperview()
            make.top.equalTo((self.gridImgView1?.snp.bottom)!).offset(10)
        }
        
        
        self.gridStatusImgView1 = UIImageView.init(image: imageNamed("icon_platform_maintain"))
        self.gridBtn1?.addSubview(self.gridStatusImgView1!)
        self.gridStatusImgView1?.snp.makeConstraints{(make) in
            make.width.equalTo(28.5)
            make.height.equalTo(28.5)
            make.right.equalToSuperview()
            make.top.equalTo(0)
        }
        
        self.girdLineView1 = UIView.init()
        self.girdLineView1?.backgroundColor = HexRGBAlpha(0xdddddd,1)
        self.contentView.addSubview(self.girdLineView1!)
        self.girdLineView1?.snp.makeConstraints{(make) in
            make.width.equalTo(0.5)
            make.height.equalToSuperview()
            make.top.equalToSuperview()
            make.left.equalTo((self.gridBtn1?.snp.right)!)
        }
        
        
        let gridBtn2 = UIButton.init(type: .custom)
        gridBtn2.setTitleColor(UIColor.black, for: .normal)
        self.contentView.addSubview(gridBtn2)
        gridBtn2.snp.makeConstraints{(make) in
            make.width.equalTo(kScreenWidth/4)
            make.left.equalTo((self.girdLineView1?.snp.right)!)
            make.height.equalToSuperview()
            make.top.equalToSuperview()
        }
         self.gridBtn2 = gridBtn2
        
        self.gridImgView2 = UIImageView.init(image: imageNamed("img_default_60"))
        self.gridBtn2?.addSubview(self.gridImgView2!)
        self.gridImgView2?.snp.makeConstraints{(make) in
            make.width.equalTo(33)
            make.height.equalTo(33)
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(15)
        }
        
        self.gridLabel2 = UILabel.init()
        self.gridLabel2?.font = UIFont.systemFont(ofSize: 14)
        gridLabel2?.textColor = UIColor.black
        gridLabel2?.textAlignment = .center
        self.gridBtn2?.addSubview(self.gridLabel2!)
        self.gridLabel2?.snp.makeConstraints{(make) in
            make.width.equalToSuperview()
            make.height.equalTo(20)
            make.centerX.equalToSuperview()
            make.top.equalTo((self.gridImgView2?.snp.bottom)!).offset(10)
        }
        
        self.gridStatusImgView2 = UIImageView.init(image: imageNamed("icon_platform_maintain"))
        self.gridBtn2?.addSubview(self.gridStatusImgView2!)
        self.gridStatusImgView2?.snp.makeConstraints{(make) in
            make.width.equalTo(28.5)
            make.height.equalTo(28.5)
            make.right.equalToSuperview()
            make.top.equalTo(0)
        }
        
        self.girdLineView2 = UIView.init()
        self.girdLineView2?.backgroundColor = HexRGBAlpha(0xdddddd,1)
        self.contentView.addSubview(self.girdLineView2!)
        self.girdLineView2?.snp.makeConstraints{(make) in
            make.width.equalTo(0.5)
            make.height.equalToSuperview()
            make.top.equalToSuperview()
            make.left.equalTo((self.gridBtn2?.snp.right)!)
        }
        
        
        let gridBtn3 = UIButton.init(type: .custom)
         self.contentView.addSubview(gridBtn3)
        gridBtn3.snp.makeConstraints{(make) in
            make.width.equalTo(kScreenWidth/4)
            make.left.equalTo((self.girdLineView2?.snp.right)!)
            make.height.equalToSuperview()
            make.top.equalToSuperview()
        }
          self.gridBtn3 = gridBtn3
        
        self.gridImgView3 = UIImageView.init(image: imageNamed("img_default_60"))
        self.gridBtn3?.addSubview(self.gridImgView3!)
        self.gridImgView3?.snp.makeConstraints{(make) in
            make.width.equalTo(33)
            make.height.equalTo(33)
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(15)
        }
        
        self.gridLabel3 = UILabel.init()
        self.gridLabel3?.font = UIFont.systemFont(ofSize: 14)
        gridLabel3?.textColor = UIColor.black
        gridLabel3?.textAlignment = .center
        self.gridBtn3?.addSubview(self.gridLabel3!)
        self.gridLabel3?.snp.makeConstraints{(make) in
            make.width.equalToSuperview()
            make.height.equalTo(20)
            make.centerX.equalToSuperview()
            make.top.equalTo((self.gridImgView3?.snp.bottom)!).offset(10)
        }
        
        self.gridStatusImgView3 = UIImageView.init(image: imageNamed("icon_platform_maintain"))
        self.gridBtn3?.addSubview(self.gridStatusImgView3!)
        self.gridStatusImgView3?.snp.makeConstraints{(make) in
            make.width.equalTo(28.5)
            make.height.equalTo(28.5)
            make.right.equalToSuperview()
            make.top.equalTo(0)
        }
        
        self.girdLineView3 = UIView.init()
        self.girdLineView3?.backgroundColor = HexRGBAlpha(0xdddddd,1)
        self.contentView.addSubview(self.girdLineView3!)
        self.girdLineView3?.snp.makeConstraints{(make) in
            make.width.equalTo(0.5)
            make.height.equalToSuperview()
            make.top.equalToSuperview()
             make.left.equalTo((self.gridBtn3?.snp.right)!)
        }
        
        
        let gridBtn4 = UIButton.init(type: .custom)
         self.contentView.addSubview(gridBtn4)
        gridBtn4.snp.makeConstraints{(make) in
            make.width.equalTo(kScreenWidth/4)
            make.left.equalTo(gridBtn3.snp.right)
            make.height.equalToSuperview()
            make.top.equalToSuperview()
        }
         self.gridBtn4 = gridBtn4
        self.gridImgView4 = UIImageView.init(image: imageNamed("img_default_60"))
        self.gridBtn4?.addSubview(self.gridImgView4!)
        self.gridImgView4?.snp.makeConstraints{(make) in
            make.width.equalTo(33)
            make.height.equalTo(33)
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(15)
        }
        
        self.gridLabel4 = UILabel.init()
        self.gridLabel4?.font = UIFont.systemFont(ofSize: 14)
        gridLabel4?.textColor = UIColor.black
        gridLabel4?.textAlignment = .center
        self.gridBtn4?.addSubview(self.gridLabel4!)
        self.gridLabel4?.snp.makeConstraints{(make) in
            make.width.equalToSuperview()
            make.height.equalTo(20)
            make.centerX.equalToSuperview()
            make.top.equalTo((self.gridImgView4?.snp.bottom)!).offset(10)
        }
        
        self.gridStatusImgView4 = UIImageView.init(image: imageNamed("icon_platform_maintain"))
        self.gridBtn4?.addSubview(self.gridStatusImgView4!)
        self.gridStatusImgView4?.snp.makeConstraints{(make) in
            make.width.equalTo(28.5)
            make.height.equalTo(28.5)
            make.right.equalToSuperview()
            make.top.equalTo(0)
        }
        
        self.girdLineViewX = UIView.init()
        self.girdLineViewX?.backgroundColor = HexRGBAlpha(0xdddddd,1)
        self.contentView.addSubview(self.girdLineViewX!)
        self.girdLineViewX?.snp.makeConstraints{(make) in
            make.width.equalToSuperview()
            make.height.equalTo(0.5)
            make.bottom.equalToSuperview()
        }
    }
    func setData(dict1:[String:Any]?,dict2:[String:Any]?,dict3:[String:Any]?,dict4:[String:Any]?) -> Void {
        self.gridBtn1?.isHidden = true
        self.gridBtn2?.isHidden = true
        self.gridBtn3?.isHidden = true
        self.gridBtn4?.isHidden = true
        if let hall = getHall(withId: dict1?["id"] as! String){
            if let status = hall["status"] as? Int{
                self.gridStatusImgView1?.isHidden = true
                if status == 0 {
                    self.gridStatusImgView1?.isHidden = false
                }
            }
        }
        
        if let hall = getHall(withId: dict2?["id"] as! String){
            if let status = hall["status"] as? Int{
                self.gridStatusImgView2?.isHidden = true
                if status == 0 {
                    self.gridStatusImgView2?.isHidden = false
                }
            }
        }
        
        if let hall = getHall(withId: dict3?["id"] as! String){
            if let status = hall["status"] as? Int{
                self.gridStatusImgView3?.isHidden = true
                if status == 0 {
                    self.gridStatusImgView3?.isHidden = false
                }
            }
        }
        
        if let hall = getHall(withId: dict4?["id"] as! String){
            if let status = hall["status"] as? Int{
                self.gridStatusImgView4?.isHidden = true
                if status == 0 {
                    self.gridStatusImgView4?.isHidden = false
                }
            }
        }
        if dict1 != nil {
            self.gridBtn1?.isHidden = false
             self.gridImgView1?.sd_setImage(with: kMainUrl((dict1?["imgNameIOS"] as? String) ?? ""), placeholderImage: imageNamed("img_default_60"), options: .refreshCached, completed: nil)
            self.gridLabel1?.text = dict1?["name"] as? String
        }
    
        if dict2 != nil {
            self.gridBtn2?.isHidden = false
            self.gridImgView2?.sd_setImage(with: kMainUrl((dict2?["imgNameIOS"] as? String) ?? ""), placeholderImage: imageNamed("img_default_60"), options: .refreshCached, completed: nil)
            self.gridLabel2?.text = dict2?["name"] as? String
        }
        
        if dict3 != nil {
            self.gridBtn3?.isHidden = false
            self.gridImgView3?.sd_setImage(with: kMainUrl((dict3?["imgNameIOS"] as? String) ?? ""), placeholderImage: imageNamed("img_default_60"), options: .refreshCached, completed: nil)
            self.gridLabel3?.text = dict3?["name"] as? String
        }
        
        if dict4 != nil {
            self.gridBtn4?.isHidden = false
            self.gridImgView4?.sd_setImage(with: kMainUrl((dict4?["imgNameIOS"] as? String) ?? ""), placeholderImage: imageNamed("img_default_60"), options: .refreshCached, completed: nil)
            self.gridLabel4?.text = dict4?["name"] as? String
        }
    }
    
    func getHall(withId: String) -> [String:Any]? {
        if let hallInfoData = GlobalInfo.hallInfoData {
            for hall in hallInfoData {
                let id = hall["id"] as! String
                if id == withId {
                    return hall
                }
            }
        }
        return nil
    }
}
